package jena.examples.rdf;



import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;


public class DataSparQL {
	
	public static void main(String[] args) {
		// Create an empty in-memory model and populate it from the graph
		Model DataModel = ModelFactory.createDefaultModel();
		DataModel.read("file:/Users/DANsarti/TBCFreeWorkspace/sarti/data.owl");
		DataModel.read("file:/Users/DANsarti/TBCFreeWorkspace/sarti/protocol.owl");
		DataModel.read("file:/Users/DANsarti/TBCFreeWorkspace/sarti/device.owl");
		System.out.println("using SPARQL");
		
		// Create a new query, FindVariable, to locate a variable in NMEAs data set
		String FindDataSet = DataSet(data.sea_water_temperature);
		
		String FindInterface =  //FindVariableInterface(data.sea_water_temperature);
		                      //EthernetInterface(data.sea_water_temperature);
			                   Device(data.sea_water_temperature);
			                  //DataSetInterface(data.DataStreamNMEA_CTD);
			                  //VariableInterface(data.sea_water_temperature);
		
		// Create a new query, DataSetVariable, to find all the variables from a NMEA data set
		String FindVariables = BaseVariable(data.DataStreamNMEA_CTD);
	    
	    /**
	     * 
	     */
	    Query ProtocolQuery = QueryFactory.create(FindInterface);//FindVariable);

		// Execute the query and obtain results
		QueryExecution ProtocolQe = QueryExecutionFactory.create(ProtocolQuery, DataModel);
		ResultSet results = ProtocolQe.execSelect();

		// Output query results	
		System.out.println(ResultSetFormatter.asText(results));
	    //ResultSetFormatter.out(System.out, results, ProtocolQuery);
		
		// Important - free up resources used running the query
	    ProtocolQe.close();
		
	}
	/** 
	 * Method to create the Query to find a variable in all the NMEAs from a observation system
	 */
	static String DataSet (Resource BaseVariable)
	{
		String DataSet = 
			"SELECT  ?dataset "+
			"WHERE { ?dataset <"+
			data.hasDataSetVariable + "> ?s. ?s <"  +
			data.xsvData + "> <"+
			BaseVariable +">  }";
		
		return DataSet;
		
	}
	/** 
	 * Method to create the Query to find all variable in a NMEA from a observation system
	 */
	static String BaseVariable (Resource DataStreamNMEA)
	{
		String Variables = 
			"SELECT  ?variable "+
			"WHERE { <"+
			DataStreamNMEA + "> <"+ 
			data.hasDataSetVariable +"> ?o. ?o <"  +
			data.xsvData + "> ?variable  }";
		
		return Variables;
		
	}
	
	/** 
	 * Method to create the Query to find the interface for a variable that we look
	 */
	static String Interface (Resource BaseVariable)
	{
		String Interface = 
			"SELECT  ?interface "+
			"WHERE { ?protocol <"+ 
			protocol.hasInterface +"> ?interface. ?protocol <"+ 
			protocol.hasDataSet +"> ?dataset. ?dataset <"+
			data.hasDataSetVariable + "> ?s. ?s <"  +
			data.xsvData + "> <"+
			BaseVariable +">  }";
		
		return Interface;
		
	}
	/** 
	 * Method to create the Query to find the interface for a data set that we look
	 */
	static String DataSetInterface (Resource DataStreamNMEA)
	{
		String Interface = 
			"SELECT  ?interface "+
			"WHERE { ?protocol <"+ 
			protocol.hasInterface +"> ?interface. ?protocol <"+ 
			protocol.hasDataSet + "> <"+
			DataStreamNMEA +">  }";
		
		return Interface;
		
	}
	
	/** 
	 * Method to create the Query to find the Ethernet interface address and port
	 */
	static String EthernetInterface (Resource BaseVariable)
	{
		String EthernetInterface = 
			"SELECT  ?EthernetInterface ?address ?port "+
			"WHERE { ?EthernetInterface <"+ 
			protocol.isEthernetInterfaceOf +"> ?protocol. ?protocol <"+ 
			protocol.hasDataSet +"> ?dataset. ?dataset <"+
			data.hasDataSetVariable + "> ?variable. ?variable <"  +
			data.xsvData + "> <"+
			BaseVariable +">. " +
					"{ ?EthernetInterface <" +
			protocol.hasAddress + "> ?address. }" +
			        "{ ?EthernetInterface <" +
			protocol.hasPort + "> ?port. }" +
					"}";
		return EthernetInterface;
		
	}
	
	/** 
	 * Method to create the Query to find the Serial interface parameter
	 */
	static String SerialInterface (Resource BaseVariable)
	{
		String SerialInterface = 
			"SELECT  ?SerialInterface ?port ?baudRate ?bitLength ?flowControl ?parity ?stopBit "+
			"WHERE { ?SerialInterface <"  + 
			protocol.isSerialInterfaceOf +"> ?protocol. ?protocol <"+ 
			protocol.hasDataSet +"> ?dataset. ?dataset <"+
			data.hasDataSetVariable + "> ?variable. ?variable <"  +
			data.xsvData + "> <"+
			BaseVariable +">. " +
			        "{ ?SerialInterface <" +
			protocol.hasPort + "> ?port. }" +
			        "{ ?SerialInterface <" +
			protocol.hasBaudRate + "> ?baudRate. }" +
	        		"{ ?interface <" +
			protocol.hasDataBitLength + "> ?bitLength. }" +
					"{ ?SerialInterface <" +
			protocol.hasFlowControl + "> ?flowControl. }" +
					"{ ?SerialInterface <" +
			protocol.hasParity + "> ?parity. }" +
					"{ ?SerialInterface <" +
			protocol.hasStopBit + "> ?stopBit. }" +
					"}";
		return SerialInterface;
		
	}
	/** 
	 * Method to create the Query to find the devices that offers variable that we look
	 */
	static String Device (Resource BaseVariable)
	{
		String Device = 
			"SELECT  ?device "+
			"WHERE { ?device <"+ 
			device.hasProtocol +"> ?protocol. ?protocol <"+ 
			protocol.hasDataSet +"> ?dataset. ?dataset <"+
			data.hasDataSetVariable + "> ?s. ?s <"  +
			data.xsvData + "> <"+
			BaseVariable +">  }";
		
		return Device;
		
	}
	
}
